package com.pro.addresslist.ui.activity;

import java.util.ArrayList;

import com.pro.addresslist.R;
import com.pro.addresslist.ui.common.BaseActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SendMsgActivity extends BaseActivity {
private EditText etrec;//收件人
private EditText etcon;//内容
private Button BTN_sendmessage;
private ArrayList<String> pharr ;
private String Showetph = "";//显示联系人列表

	@Override
	public int getLayout() {
		// TODO Auto-generated method stub
		return R.layout.sendmsg;
	}

	private void inintview() {
		etrec = (EditText)this.findViewById(R.id.etrec);
		etcon = (EditText)this.findViewById(R.id.etcon);
		BTN_sendmessage = (Button)this.findViewById(R.id.BTN_sendmessage);
		BTN_sendmessage.setOnClickListener(this);
	}

	@Override
	public void OnActCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		setTtile("短信发送");
		setLeftImageBack();
		inintview();
		Intent pint = this.getIntent();
		pharr = new ArrayList<String>();
		pharr =	pint.getStringArrayListExtra("telNum");
	
		if(pharr!=null){
			for(int i=0;i< pharr.size() ; i++){
				if(i > 1){
					break;
				}
				Showetph +=  pharr.get(i)+",";
			}
			Showetph = Showetph.trim().substring(0,Showetph.length()-1);
			etrec.setText(Showetph+"...等"+pharr.size()+"位联系人");
		}
	}

	@Override
	public void OnViewClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.BTN_sendmessage:
			if(etcon.length()<=0){
				Toast.makeText(getApplicationContext(), "短信内容不能为空！", Toast.LENGTH_LONG).show();
			}else{
				if(pharr.size()<=0){
					Toast.makeText(getApplicationContext(), "请先添加联系人！", Toast.LENGTH_LONG).show();
				}
				else{
					try{
						SmsManager smsManager = SmsManager.getDefault();
						showProgressDialog();
						for(int i = 0; i <pharr.size(); i++ ){
							smsManager.sendTextMessage(pharr.get(i).trim(), null, etcon.getText().toString(), null, null);
						}	
						disMissDialog();
						Toast.makeText(getApplicationContext(), "信息发送成功！", Toast.LENGTH_LONG).show();
						finish();

					}catch(Exception ex){
						Toast.makeText(getApplicationContext(), ex.getMessage(), Toast.LENGTH_LONG).show();
					}
							
				}
			}
			break;

		default:
			break;
		}
	}

}
